<template>
  <div class="contacts-block-wrap" v-loading="loading">
    <div class="top-operate">
      <div class="left-tab">
        <div class="tab-item pointer" :class="activeTab=='list'?'active-tab-class':''" @click="tabChange('list')">列表视图<i class="tab-icon el-icon-s-operation"></i></div>
        <div class="tab-item pointer" :class="activeTab=='architecture'?'active-tab-class':''" @click="tabChange('architecture')">
          架构视图
          <i class="el-icon-c-scale-to-original tab-icon"></i>
        </div>
      </div>
      <el-button icon="el-icon-plus" @click="addCustomer">新建联系人</el-button>
    </div>
    <div class="listTable" v-if="activeTab=='list'">
      <el-table :data="tableData" :cell-class-name="cellClass" stripe @cell-click="tableDataClick">
        <el-table-column
          show-overflow-tooltip
          type="selection"
          align="center"
          width="55"
        >
      </el-table-column>
          
        <el-table-column
          v-for="(item, index) in headList"
          :key="index"
          :prop="item.prop"
          :label="item.label"
          show-overflow-tooltip
        />
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <!-- <flexbox justify="center"> -->
              <span v-if="true" class="chief">
                <i class="el-icon-circle-check fist-icon" />首要联系人</span
              >
              <el-button
                v-else-if="!isSeas"
                class="set-chief-btn"
                type="primary-text"
                @click.native="setChieflyContacts(scope)"
                >设为首要联系人</el-button
              >
            <!-- </flexbox> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 关系树 -->
    <div class="" v-if="activeTab=='architecture'">
      <vue2-org-tree
        v-for="(item,index) in treeList"
        :key="index"
        :data="item"
        :label-class-name="vue2orgTreeClass"
        :props="{label: 'name', children: 'children', expand: 'expand'}"
        :render-content="renderContent"
        @on-node-click="orgNodeClick"
      />
    </div>
     <!-- 新建联系人 -->
    <addDialog ref="addDialog" :action="createAction" @saveSubmit="saveSubmitFun" :addType="addType" dialogTitle="新建联系人" />
    <!-- 抽屉 -->
    <contactDrawer ref="contactDrawer"></contactDrawer>
  </div>
</template>
<script>
//联系人抽屉
import contactDrawer from '@/components/contactDrawer/index'
//新增联系人
import addDialog from '@/components/DialogCustom/addDialog.vue'
import { queryContactsPageList, addContacts } from "@/api/contacts/contacts";
export default {
  components: { contactDrawer,addDialog },
  data() {
    return {
      headList: [
        { prop: "name", width: "200", label: "姓名" },
        { prop: "mobile", width: "200", label: "手机" },
        { prop: "post", width: "200", label: "职务" },
        { prop: "parentContactsId", width: "200", label: "直属上级" },
      ],
      tableData: [],
      activeTab: 'list',
      // contactsId:'',
      treeList:[],
      loading:false,
      addType: this.$GlobalStatus.tableType.Contacts,
      createAction:{
        
      }
    };
  },
  mounted() {
    this.queryListFun()
  },
  methods:{
   
    tableDataClick(row, column, cell, event){
      if(column.label=='姓名'){
        console.log(row)
        this.$refs.contactDrawer.show(row);
        // this.$refs.addDialog.showFun();
      }
    },
    queryListFun() {
      // var params = {
      //   search: "1",
      //   poolId: 1,
      //   sceneId: 1,
      //   label: 2,
      //   sortField: "1",
      //   order: 1,
      //   pageNum: 1,
      //   pageSize: 15
      // }
      this.loading = true;
      var params = {
        label: 3,
        pageNum: 1,
        pageSize: 15
      }
      queryContactsPageList(params).then(res => {
        // console.log("表格数据"+res.data)
        this.tableData = res.data.records;
        this.treeList = res.data.records;
        this.total = res.data.total;
        this.loading = false;
      }).catch(error => {
        this.loading = false;
      })
    },
    // 新建联系人
    addCustomer() {
      this.createAction = {
        type: 'save',
        id: '',
        data: {}
      }
      this.$refs.addDialog.showFun();
    },
     //保存
     saveSubmitFun(params) {
      this.loading = true;
      params.entity.customerId = 1
      params.entity.parentContactsId = 1
      addContacts(params).then(res => {
        this.loading = false;
        this.queryList();
        this.$refs.addDialog.closeFun();
      }).catch(error => {
        this.loading = false;
      })
    },
    tabChange(type){
      this.activeTab = type
    },
    //通过回调控制class
    cellClass({ row, column, rowIndex, columnIndex }) {
      if (column.property === 'name') {
        return 'look--underline'
      } else {
        return ''
      }
    },
    orgNodeClick(e, data) {
      // this.detailId = data.contactsId
      // this.showFullDetail = true
    },
    vue2orgTreeClass(data) {
      return 'vue2-org-tree-default'
    },
    renderContent(h, data) {
      return `${data.name}`
    },
  }
};
</script>
<style lang="scss" scoped>
.contacts-block-wrap {
  .top-operate{
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
    .left-tab{
      background: #ebecf0;
      border-radius: 3px;
      line-height: 1.3333;
      text-transform: uppercase;
      display: flex;
      padding: 4px;
    }
    .active-tab-class{
      background-color: #fff;
      border-radius: 3px;
      box-shadow: 0 3px 5px rgba(13,20,36,.18),0 0 1px rgba(13,20,36,.29)!important;
    }
    .tab-item{
      display: flex;
      align-items: center;
      font-size: 12px;
      font-weight: 500;
      padding: 0 5px;
      line-height: 24px;
      text-transform: uppercase;
    }
    .tab-item:not(:first-child){
      margin-left: 4px;
    }
    .tab-icon{
      margin-left: 2px;
    }
  }
  .fist-icon{
    margin-right: 3px;
    font-size: 14px;
    color: #36b37e;
  }

  .vue2-org-tree-default {
    font-size: 12px;
    font-weight: 500;
    color: #172b4d;
    background-color: #f4f5f7;
    box-shadow: none !important;
  }
}
</style>